<?php
/**
 * Product: rdc_analysis.
 * Date: 18-7-25
 * Time: 下午4:12
 */

namespace server;

// use \Phalcon\Di\FactoryDefault;
use enums\HttpServerEnums;
use libraries\CommUtils;
use enums\ServiceEnums;

class HttpServer
{
    use \server\WorkerStart;
    use \server\Request;
    use \server\ErrorHandler;
    use \server\TasksMgrTrait;

    public static $instance;

    private $_http;

    // 	public static $get;
    // 	public static $post;
    // 	public static $header;
    // 	public static $server;

    public static $response;

    // 	public static $session;

    //public static $httpServer = null;

    private $application;

    public static $cookie;

    private $_taskMgr = null;

    private $_swooleCfg = null;

    private $di;



    private function __construct( $arrCfg = null )
    {
        $this->initSwooleHttpServer( $arrCfg );

        $this->initTaskMgr();

        $this->_http->on('WorkerStart', array($this, 'onWorkerStart'));

        $this->_http->on( 'WorkerStop', array( $this, 'onWorkerStop' ) );

        $this->_http->on( 'Shutdown', array( $this, 'onShutdown' ) );

        //version < 1.8.6
        if (version_compare(SWOOLE_VERSION, '1.8.6') < 0) {
            $this->_http->setGlobal(HTTP_GLOBAL_ALL);
        }

        $this->_http->on('Start', array( $this, 'onStart' ) );

        $this->_http->on('request', array( $this, 'onRequest' ));

        $this->_http->on( 'task', array( $this, 'onTask' ) );

        $this->_http->on( 'finish', array( $this, 'onFinish' ));

        $this->_http->start();

    }

    private function initTaskMgr()
    {
        assert( $this->_http );

        $this->_taskMgr = new \server\TaskMgr( $this->_http );

    }

    public function onFinish($server, $task_id, $params)
    {
        echo 'finished', PHP_EOL;

        $arrParams = json_decode($params);

        $this->_taskMgr->resetTask($arrParams);
    }


    public function onStart( $server )
    {
        echo 'Starting...' . PHP_EOL;
        if (php_uname('s') != 'Darwin')
        {
            cli_set_process_title( HttpServerEnums::MASTER_PROCESS_TITLE );
        }
    }

    public function onShutdown( $server )
    {
        if( isset( $this->_swooleCfg->pid_file ) )
        {
            unlink( $this->_swooleCfg->pid_file );
        }
    }

    public function onTask( $server, $task_id, $reactor_id, $params )
    {
        $arrParams = json_decode($params);

        if (array_key_exists($arrParams->taskInfo->taskLabel, $this->_taskMgr->getArrValidTasks()))
        {

            $this->_taskMgr->changeWorkerStatus( $server, $arrParams->taskInfo );

            $this->_taskMgr->doTaskWork( $server, $task_id, $reactor_id, $arrParams, $this->application->getDI() );

            $server->finish(json_encode($arrParams->taskInfo));
        }
        else
        {
            echo 'worker not exist!', PHP_EOL;
        }
    }


    private function initSwooleHttpServer( $arrCfg )
    {
        $this->_swooleCfg = include dirname(__DIR__) . '/config/swoole.php';

        $this->_http = new \swoole_http_server( $this->_swooleCfg->host, $this->_swooleCfg->port );

        $this->_http->set( array_merge( $this->_swooleCfg->toArray(), $arrCfg ));

        return $this->_http;
    }


    public function onWorkerStop( $server, $worker_id )
    {
        echo 'worker: ', $worker_id, 'stopped', PHP_EOL;
    }










    public static function getInstance( $arrCfg = null )
    {
        if (!self::$instance)
        {
            self::$instance = new HttpServer( $arrCfg );
        }

        return self::$instance;
    }

}